package docker

import (
	"fmt"
	"pdocker/config"
	. "pdocker/tool/docker"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/swarm"
)

func CreateSwarmService(container *config.CtfContainer) {
	replicas := uint64(1)
	_, _ = Cli.ServiceCreate(Ctx, swarm.ServiceSpec{
		Annotations: swarm.Annotations{
			Name:   "",
			Labels: nil,
		},
		TaskTemplate: swarm.TaskSpec{
			ContainerSpec: &swarm.ContainerSpec{
				Image:           container.Image,
				Command:         nil,
				Args:            nil,
				Hostname:        "",
				Env:             nil,
				Dir:             "",
				User:            "",
				Groups:          nil,
				Privileges:      nil,
				Init:            nil,
				StopSignal:      "",
				TTY:             false,
				OpenStdin:       false,
				ReadOnly:        false,
				Mounts:          nil,
				StopGracePeriod: nil,
				Healthcheck:     nil,
				Hosts:           nil,
				DNSConfig:       nil,
				Secrets:         nil,
				Configs:         nil,
				Isolation:       "",
				Sysctls:         nil,
				Capabilities:    nil,
			},
			PluginSpec:            nil,
			NetworkAttachmentSpec: nil,
			Resources:             nil,
			RestartPolicy:         nil,
			Placement:             nil,
			Networks:              nil,
			LogDriver:             nil,
			ForceUpdate:           0,
			Runtime:               "",
		},
		Mode: swarm.ServiceMode{
			Replicated: &swarm.ReplicatedService{Replicas: &replicas},
		},
		UpdateConfig:   nil,
		RollbackConfig: nil,
		Networks: []swarm.NetworkAttachmentConfig{
			swarm.NetworkAttachmentConfig{
				Target:     "",
				Aliases:    nil,
				DriverOpts: nil,
			},
		},
		EndpointSpec: &swarm.EndpointSpec{
			Mode: "",
			Ports: []swarm.PortConfig{
				swarm.PortConfig{
					Name:          "",
					Protocol:      "",
					TargetPort:    0,
					PublishedPort: 0,
					PublishMode:   "",
				},
			},
		},
	}, types.ServiceCreateOptions{})
}

func StartSwarmService() {
	//_, _ = Cli.ServiceUpdate()
}

func ListServices() {
	services, err := Cli.ServiceList(Ctx, types.ServiceListOptions{})
	if err != nil {
		panic(err)
	}
	for service := range services {
		fmt.Println(service)
	}
}
